package com.power.taskcenter.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.power.taskcenter.entity.DynamicTaskConfig;
import com.power.taskcenter.entity.DynamicTaskLogs;
import com.power.taskcenter.mapper.DynamicTaskConfigMapper;
import com.power.taskcenter.mapper.DynamicTaskLogsMapper;
import com.power.taskcenter.service.IDynamicTaskConfigService;
import com.power.taskcenter.service.IDynamicTaskLogsService;
import com.power.taskcenter.util.BusinessUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class DynamicTaskLogsServiceImpl extends ServiceImpl<DynamicTaskLogsMapper, DynamicTaskLogs> implements IDynamicTaskLogsService {

    @Autowired
    private IDynamicTaskConfigService dynamicTaskConfigService;

    @Override
    public void saveLogs(DynamicTaskConfig obj, String logContent) {
        DynamicTaskLogs logs = new DynamicTaskLogs();
        logs.setTaskId(obj.getId());
        logs.setTaskBean(obj.getBeanName());
        logs.setTaskName(obj.getName());
        logs.setTaskCron(obj.getCron());
        logs.setLogContent(logContent);
        logs.setCreateDate(new Date());
        this.save(logs);
    }

    @Override
    public void saveLogs(String beanName, String logContent) {

        if (!StringUtils.isBlank(beanName)) {

            QueryWrapper<DynamicTaskConfig> wrapper = new QueryWrapper<>();
            wrapper.eq("bean_name", beanName);
            DynamicTaskConfig dynamicTaskConfig =    dynamicTaskConfigService.getOne(wrapper);
            if (dynamicTaskConfig !=null){

                DynamicTaskLogs logs = new DynamicTaskLogs();
                logs.setTaskId(dynamicTaskConfig.getId());
                logs.setTaskBean(dynamicTaskConfig.getBeanName());
                logs.setTaskName(dynamicTaskConfig.getName());
                logs.setTaskCron(dynamicTaskConfig.getCron());
                logs.setLogContent(logContent);
                logs.setCreateDate(new Date());
                this.save(logs);
            }
        }

    }

    @Override
    public void saveLogs(String beanName, Date d) {
        if (!StringUtils.isBlank(beanName)) {

            QueryWrapper<DynamicTaskConfig> wrapper = new QueryWrapper<>();
            wrapper.eq("bean_name", beanName);
            DynamicTaskConfig dynamicTaskConfig =    dynamicTaskConfigService.getOne(wrapper);
            if (dynamicTaskConfig !=null){

                DynamicTaskLogs logs = new DynamicTaskLogs();
                logs.setTaskId(dynamicTaskConfig.getId());
                logs.setTaskBean(dynamicTaskConfig.getBeanName());
                logs.setTaskName(dynamicTaskConfig.getName());
                logs.setTaskCron(dynamicTaskConfig.getCron());
                logs.setLogContent("下次预计执行时间："+ BusinessUtils.getNextDateTimeStr(dynamicTaskConfig.getCron(),d));
                logs.setCreateDate(new Date());
                this.save(logs);
            }
        }
    }



}
